/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.grid.util;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>Base class for all grid filter types<br />
 * Supports maximum of two clauses. <br />
 * </p>
 * <p><a href="BaseFilter.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.9 $ $Date: 2006/08/03 10:08:21 $
 */
public abstract class BaseFilter implements GridFilter {

	// ~ Instance variables

	protected final Log log = LogFactory.getLog(BaseFilter.class);

	/**
	 * Name of field this filter is associated with
	 */
	protected String fieldName;

	/**
	 * List of identifiers of rowIterators this filter is applicable to
	 */
	protected List rowIterators = Collections.synchronizedList(new LinkedList());

	/**
	 * First filter clause
	 */
	private String firstClause = new String();

	/**
	 * Second filter clause
	 */
	private String secondClause = new String();

	/**
	 * String with first and second clauses combined
	 */
	private String clause = new String();

	// ~ Business methods

	/**
	 * Creates new instance of BaseFilter
	 *
	 * @param fieldName Name of field to associate filter with
	 */
	public BaseFilter(String fieldName) {
		this.fieldName = fieldName;
	}

    /**
     * Returns name of field filter is associated with
     *
     * @return name of field
     */
	public String getFieldName() {
		return fieldName;
	}

    /**
     * Returns list of identifiers of rowIterators this filter is applicable to
     *
     * @return list of identifiers of rowIterators
     */
	public List getRowIterators() {
		return rowIterators;
	}

    /**
     * Sets list of identifiers of rowIterators this filter is applicable to
     *
     * @param rowIterators list of identifiers of rowIterators to set
     */
	public void setRowIterators(List rowIterators) {
		this.rowIterators = rowIterators;
	}

    /**
     * Adds row iterator ID to list of identifiers of rowIterators this filter is applicable to
     *
     * @param rowIteratorId row iterator ID
     */
	public void addRowIterator(String rowIteratorId) {
		this.rowIterators.add(rowIteratorId);
	}

	/**
	 * Creates comparison with <code>null</code>
	 *
	 * @param condition Condition to set (is not null or is not null)
	 * @return String contains comparison with null
	 */
	protected String createNullComparison(String condition) {
		return fieldName + " " + condition;
	}

	/**
	 * Sets first clause
	 *
	 * @param firstClause Clause to set
	 */
	protected void setFirstClause(String firstClause) {
		this.firstClause = firstClause;
	}

	/**
	 * Sets second clause
	 *
	 * @param secondClause Clause to set
	 * @param logicCopula  Logic copula between first and second condition ("and" or "or").
	 */
	protected void setSecondClause(String secondClause, String logicCopula) {
		this.secondClause = " " + logicCopula + " " + secondClause;
	}

	/**
	 * Returns clause constructed from two conditions, if
	 * this filter is enabled for iterator with specified ID. Otherwise
     * <code>null is returned.</code>
	 *
	 * @return String with first and second clauses combined
	 */
	public String getClause(String rowIteratorId) {

		if ( (rowIteratorId == null) || (rowIterators.size() == 0) || (rowIterators.contains(rowIteratorId)) ) {
			if ( firstClause.length() != 0 ) {
				clause = "(" + firstClause + secondClause + ")";
			} else {
				clause = null;
			}
			return clause;
		} else {
			return null;
		}
	}

	/**
	 * Returns clause constructed from two conditions
	 *
	 * @return clause constructed from two conditions
	 */
	public String getClause() {
		return getClause(null);
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}
}
